from common.flask_package import *
from model.log import *
from common.my_msg import *
from common.my_fun import *
from datetime import datetime,timedelta
import json

bp_log = Blueprint('log', __name__, url_prefix='/log')

@bp_log.route('/get', methods=['GET'])
def log_get():
    if request.method=='GET':
        page = request.args.get('page', default=1)
        log_time = request.args.get('log_time')
        page = request.args.get('page', default=1)
        # ''传递过来是一个字符不是空
        if log_time is not None and log_time != '':
            result = Log.log_time_search(log_time)
            count = 1
        else:
            result, count = Log.query_log_all(page)
        log_list = []
        try:
            for i in result:
                item = {
                    'log_id': i.log_id,
                    'log_user': i.log_user,
                    'log_content': i.log_content,
                    'log_time': f'{i.log_time}',
                }
                log_list.append(item)

            data = {
                'code': state_ok(),
                'msg': '操作日记列表获取成功',
                'count': count,
                'data': log_list
            }
        except Exception as e:
            data = {
                'code': state_error(),
                'msg': '操作日记列表获取成功',
                'count': 1,
                'data': log_list
            }
    return Response(json.dumps(data), mimetype='application/json')


#------------------------删除操作日记-------------------------------
@bp_log.route('/del',methods=['DELETE'])
def log_del():
    if request.method=='DELETE':
        log_id=request.args.get('log_id')
        relust=Log.del_log(log_id)
        if relust==0:
            data = {
                'code': state_ok(),
                'msg': '删除成功'
            }
        else:
            data = {
                'code': state_error(),
                'msg': '删除失败'
            }
    return jsonify(data)
